﻿using Authorization.Center.Controllers;
using Authorization.Center.SqlServer;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using S.BreedingHistory.Api.Application.Commmand;

namespace S.BreedingHistory.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BreedHistoryController : BaseController
    {
        private readonly IMediator mediator;
     
        public BreedHistoryController(MySqlDbContext _db, IMediator mediator) : base(_db)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 测试Apollo是否可以使用
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult ApolloTest([FromServices] IConfiguration configuration, string key)
        {
            return Ok(configuration.GetValue<string>(key));
        }
        /// <summary>
        /// 种母历史记录表的添加
        /// </summary>
        /// <param name="breedingHistoryAddCommand"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public Task<bool> AddBreedingHistory(BreedingHistoryAddCommand breedingHistoryAddCommand)
        {
            breedingHistoryAddCommand.CreateBy = UserStrategyDto.UserTrueName;
            breedingHistoryAddCommand.CreateDate = DateTime.Now;
            return mediator.Send(breedingHistoryAddCommand);
        }
        /// <summary>
        /// 批量添加种母历史记录表
        /// </summary>
        /// <param name="breedingHistoryAddCommand"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public Task<bool> BatchAddBreedingHistory(BreedingHistoryBatchAddCommand breedingHistoryBatchAddCommand)
        {
            foreach(var item in breedingHistoryBatchAddCommand.BreedingHistoryModelsList)
            {
                item.CreateBy= UserStrategyDto.UserTrueName;
                item.CreateDate = DateTime.Now;
            }
            return mediator.Send(breedingHistoryBatchAddCommand);
        }
        /// <summary>
        /// 种母历史记录的修改
        /// </summary>
        /// <param name="breedHistoryUpdateCommand"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPut]
        public async Task<IActionResult> UpdateBreedingHistory(BreedHistoryUpdateCommand breedHistoryUpdateCommand)
        {
            breedHistoryUpdateCommand.UpdateBy = UserStrategyDto.UserTrueName;
            breedHistoryUpdateCommand.UpdateDate=DateTime.Now;
            return Ok(await mediator.Send(breedHistoryUpdateCommand));
        }
        /// <summary>
        /// 种母历史记录的逻辑删除
        /// </summary>
        /// <param name="breedHistoryLogiDelCommand"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPut]
        public Task<bool> LogicDelBreedingHistory(BreedHistoryLogiDelCommand breedHistoryLogiDelCommand)
        {
            
            breedHistoryLogiDelCommand.UpdateBy = UserStrategyDto.UserTrueName;
            breedHistoryLogiDelCommand.UpdateDate = DateTime.Now;
            return mediator.Send(breedHistoryLogiDelCommand);
        }
        [Authorize]
        [HttpGet]
        public IActionResult IgetTest()
        {
            return Ok("郭佳的服务");
        }
        /// <summary>
        /// 批量修改种母历史记录
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public Task<bool> BatchUpdateBreedHistory(BreedHistoryBatchUpdateCommand t)
        {
            foreach (var item in t.breedingHistoryModels)
            {
                item.UpdateBy = UserStrategyDto.UserTrueName;
                item.UpdateDate = DateTime.Now;
                item.IsDel = false;
            }
            return mediator.Send(t);
        }
    }
}
